/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client;


import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import server.*;

/**
 *
 * @author Java
 */
public class ClientTabThread extends Thread {

    JTabbedPane jTabbedPane1;
    JList jList1;
    JList jList2;
    ManagerInf m;
    int playerNum, gameNum;
    JLabel jLabel2;
    PairKey pairkey;

    public ClientTabThread(PairKey chainPain,JLabel jLabel2,int gameNum, int p, ManagerInf m, JTabbedPane jTabbedPane1, JList jList1, JList jList2) {
        this.m = m;
        this.jTabbedPane1 = jTabbedPane1;
        this.jList1 = jList1;
        this.jList2 = jList2;
        playerNum = p;
        this.gameNum = gameNum;
        this.jLabel2=jLabel2;
        this.pairkey = chainPain;
    }

    public void run() {
        try {
            int currentPlayers[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; // to make sure dublciate in tabs
            
            while (true) {
                if (m.isGameFinished(gameNum))
                {
                    JOptionPane.showMessageDialog(null,"The Game is finished");
                    return;
                }
                int playersNum = m.getPlayersNumber(0);
                for (int i = 0; i < playersNum; i++) { // iterate all players
                    if (currentPlayers[i] == -1) { // if player currently was not added
                        if (m.getAMap(i, gameNum) != null) { // if there is a player (player will have a map directly
                            String playerName = m.getPlayerName(i, gameNum);
                            if (this.playerNum != i) {
                                jTabbedPane1.addTab(playerName, new PlayerMapPanel(pairkey,i, playerNum,gameNum, m));
                                currentPlayers[i] = i; //
                            }
                        }
                    } else // else player currently was added
                    {
                        if (m.getAMap(i, 0) == null) { // if the player doesn't exist (not have map)
                            jTabbedPane1.remove(i);
                            jList1.setListData(m.getPlayersName(gameNum));
                            currentPlayers[i] = -1;
                        }
                    }
                }
                
                jList1.setListData(m.getPlayersName(gameNum));
                jLabel2.setText("Score: " + m.getPlayerScore(playerNum, gameNum));
                // if number of games changed
                int gamesNum = 0;
                if (gamesNum != m.getGamesName().length) {
                    gamesNum = m.getGamesName().length;
                    jList2.setListData(m.getGamesName());
                    jList2.setSelectedIndex(gameNum);
                }
                Thread.sleep(1000); // recheck every second
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
